package cc.comac.mom.datahandlers.entity.restreceive;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cc.comac.mom.dao.entity.QualityFrrFormEntityDao;
import cc.comac.mom.entity.QualityFrrFormEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
@Transactional
@Component
public class QualityFrrFormServiceDataHandler {

	@Autowired
	private QualityFrrFormEntityDao qualityFrrFormEntityDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public String insertFrrForm(QualityFrrFormEntity qualityFrrFormEntity) {
		Map<String,String> map = new HashMap<String, String>();
		map.put("status", "1");
		map.put("msg", "");
		if (StringUtils.isEmpty(qualityFrrFormEntity.getFrrNo())) {
			map.put("status", "0");
			map.put("msg", "frr编号不能为空");
			return map.toString();
		}else if (StringUtils.isEmpty(qualityFrrFormEntity.getStatus())) {
			map.put("status", "0");
			map.put("msg", "状态不能为空");
			return map.toString();
		}
		QueryWrapper<QualityFrrFormEntity> queryWrapper = new QueryWrapper<QualityFrrFormEntity>();
		queryWrapper.eq("frr_no", qualityFrrFormEntity.getFrrNo());
		QualityFrrFormEntity selectOne = qualityFrrFormEntityDao.selectOne(queryWrapper);
		if (selectOne == null) {
			baseEntityDataUtil.setBaseEntityData(qualityFrrFormEntity);
			int insert = qualityFrrFormEntityDao.insert(qualityFrrFormEntity);
			if (insert != 1) {
				map.put("status", "0");
				map.put("msg", "新增失败！");
				return map.toString();
			}
		} else {
			selectOne.setStatus(qualityFrrFormEntity.getStatus());
			baseEntityDataUtil.updateBaseEntityData(selectOne);
			int updateById = qualityFrrFormEntityDao.updateById(selectOne);
			if (updateById != 1) {
				map.put("status", "0");
				map.put("msg", "更新状态失败！");
				return map.toString();
			}
		}
		return map.toString();
	}
}
